package com.zhdl.modules.prefiresys.service.impl;


import cn.hutool.core.util.IdUtil;
import com.project.common.service.impl.BaseServiceImpl;
import com.zhdl.modules.prefiresys.entity.FactoryLocalHost;
import com.zhdl.modules.prefiresys.entity.FactoryWeeklyRisk;
import com.zhdl.modules.prefiresys.entity.QDefAreaAlarmRec;
import com.zhdl.modules.prefiresys.entity.QDetectorHistoryData;
import com.zhdl.modules.prefiresys.repository.FactoryWeeklyRiskRepository;
import com.zhdl.modules.prefiresys.service.DefAreaAlarmRecService;
import com.zhdl.modules.prefiresys.service.FactoryLocalHostService;
import com.zhdl.modules.prefiresys.service.FactoryWeeklyRiskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigInteger;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * 前七天风险趋势统计
 */
@Slf4j
@Service
public class FactoryWeeklyRiskServiceImpl extends BaseServiceImpl<String, FactoryWeeklyRisk, FactoryWeeklyRiskRepository> implements FactoryWeeklyRiskService {


    private final DefAreaAlarmRecService defAreaAlarmRecService;

    private final FactoryLocalHostService factoryLocalHostService;
    /**
     * 测试，临时接收数据
     */
    Integer test;

    /**
     * 初始化
     *
     * @param defAreaAlarmRecService
     * @param factoryLocalHostService
     */
    @Autowired
    public FactoryWeeklyRiskServiceImpl(DefAreaAlarmRecService defAreaAlarmRecService, FactoryLocalHostService factoryLocalHostService) {
        this.defAreaAlarmRecService = defAreaAlarmRecService;
        this.factoryLocalHostService = factoryLocalHostService;
    }

    /**
     * 计算七天内每天的报警数据
     */
    @Transactional
    @Override
    public void countAgoWeekAlarmData() {
        //查询 七天内报警数据量
        List<Object[]> list = defAreaAlarmRecService.statAgoWeekAlarmCount();
        //删除原来的数据，之后添加新的数据
        deleteAll();

        //一周报警数量
        Integer weekAlarmCount = 0;

        for (Object[] row : list) {

            FactoryWeeklyRisk factoryWeeklyRisk = new FactoryWeeklyRisk();
            String dateStr = (String) row[0];
            BigInteger count = (BigInteger) row[1];

            factoryWeeklyRisk.setId(IdUtil.fastSimpleUUID());
            factoryWeeklyRisk.setStatDate(dateStr);
            factoryWeeklyRisk.setAlarmCount(count.longValue());

            //添加 一天的 风险趋势信息
            insert(factoryWeeklyRisk);

            weekAlarmCount += count.intValue() - 1;
        }

        this.test = weekAlarmCount;

    }

    /**
     * 统计最近一个月报警数据
     */
    @Transactional
    @Override
    public void countLocalHostAlarmData() {

        // 报警总数
        int totalCount;

        //从alarmRec统计最近一个月报警数据
        FactoryLocalHost factoryLocalHost = factoryLocalHostService.findRecently();

        /**
         * 获取今天的时间，以及最近一个月的开始时间
         */
        LocalDate today = LocalDate.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        String startDay = today.plusMonths(-1).format(formatter);

        //报警记录表中查询
        QDefAreaAlarmRec qAlarmRec = QDefAreaAlarmRec.defAreaAlarmRec;

        /**
         * ------------------------------------------------------------------------
         * 以下为定时任务，一天一存，存放到 local_host 表中
         */

        // 4 一级报警
        totalCount = jpaQueryFactory().selectFrom(qAlarmRec)
                .where(qAlarmRec.createDate.gt(startDay).and(qAlarmRec.alarmStatus.eq((byte) 4)))
                .fetch()
                .size();
        factoryLocalHost.setMonthAlarm1(totalCount);

        // 3 二级报警
        totalCount = jpaQueryFactory().selectFrom(qAlarmRec)
                .where(qAlarmRec.createDate.gt(startDay).and(qAlarmRec.alarmStatus.eq((byte) 3)))
                .fetch()
                .size();
        factoryLocalHost.setMonthAlarm2(totalCount);

        // 2 三级报警
        totalCount = jpaQueryFactory().selectFrom(qAlarmRec)
                .where(qAlarmRec.createDate.gt(startDay).and(qAlarmRec.alarmStatus.eq((byte) 2)))
                .fetch()
                .size();
        factoryLocalHost.setMonthAlarm3(totalCount);

        //-------------------------------------------------------------------------------------


        // 最近一个月设备故障次数，不正常的都算故障
        QDetectorHistoryData qDetectorHistoryData = QDetectorHistoryData.detectorHistoryData;
        totalCount = jpaQueryFactory().selectFrom(qDetectorHistoryData)
                .where(qDetectorHistoryData.createDate.gt(startDay).and(qDetectorHistoryData.detectorUnitStateValue.notIn(0)))
                .fetch()
                .size();
        factoryLocalHost.setMonthDeviceFailCount(totalCount);

        //接收临时测试数据
        factoryLocalHost.setWeekAlarmCount(test);
        //更新本地主机的信息
        factoryLocalHostService.update(factoryLocalHost, factoryLocalHost.getId());

    }

}
